﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using vnFingerPrint.DataAccess;
using vnFingerPrint.BusinessObject;
using vnCommonGUI;
namespace vnFingerPrint.GUI
{
    public partial class frmQuanLyCongDan : Form
    {
        ImageList li = new ImageList();
        public frmQuanLyCongDan()
        {
            InitializeComponent();
            Bind_ListCongDan();
        }
        public void Bind_ListCongDan()
        {
            li.Images.Clear();
            li.ImageSize = new Size(100, 100);
            li.TransparentColor = Color.Azure;
            listCongDan.Items.Clear();
            listCongDan.LargeImageList = li;
            foreach (CongDan_Object   u in CongDan_DataAccess.SelectAll())
            {
                if (u.HoTen.IndexOf(txtSearchName.Text) < 0 && u.SoCMND.IndexOf(txtSearchName.Text) < 0) continue;
                string imgurl = Config_DataAccess.GetCongDanImagePath(u.ImageUrl);
                try
                {
                    if (u.ImageUrl != "" && u.ImageUrl != null) li.Images.Add(u.SoCMND, new Bitmap(imgurl));
                    else li.Images.Add(u.SoCMND, new Bitmap(512, 512));
                }
                catch
                { 
                }
                ListViewItem i = new ListViewItem(u.HoTen, u.SoCMND);
                i.ToolTipText = u.SoCMND;
                listCongDan.Items.Add(i);
            }
            listCongDan.Refresh();

        }
        private void txtSearchName_TextChanged(object sender, EventArgs e)
        {
            Bind_ListCongDan();
        }

        private void frmQuanLyCongDan_Load(object sender, EventArgs e)
        {
            Bind_ListCongDan();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                frmNhapCongDan frm = new frmNhapCongDan();
                frm.ShowDialog();
                Bind_ListCongDan();
            }
            catch
            {
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (listCongDan.SelectedItems.Count < 1)
            {
                MessageBox.Show("Quí vị chưa chọn công dân cần sửa thông tin!");
                return;
            }
            try
            {
                frmNhapCongDan frm = new frmNhapCongDan(listCongDan.SelectedItems[0].ToolTipText);
                frm.ShowDialog();
               // Bind_ListCongDan();
            }
            catch
            {

            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (listCongDan.SelectedItems.Count < 1)
            {
                MessageBox.Show("Quí vị chưa chọn tài khoản cần xóa!");
                return;
            }
            if (MessageBox.Show("Quí vị có chắc là mình muốn xóa tài khoản này?", "Cảnh báo", MessageBoxButtons.YesNo) == DialogResult.No) return;
            CongDan_Object delU = CongDan_DataAccess.GetObject(listCongDan.SelectedItems[0].ToolTipText);
            List<VanTay_Object> delVanTay = VanTay_DataAccess.GetObject(delU.SoCMND);
            foreach(VanTay_Object vo in delVanTay)
            {
                VanTay_DataAccess.Delete(vo);
            }
            CongDan_DataAccess.Delete(delU);
            Bind_ListCongDan();
        }

        private void listCongDan_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                frmNhapCongDan frm = new frmNhapCongDan(listCongDan.SelectedItems[0].ToolTipText);
                frm.ShowDialog();
                //Bind_ListCongDan();
            }
            catch
            {
            }
        }
    }
}
